package com.example.controller;

import com.example.domain.vo.PageResult;
import com.example.domain.vo.Result;
import com.example.service.UserListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户列表模块控制层
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserListController {

    private final UserListService userListService;

    /**
     * 获取用户列表信息
     */
    @GetMapping("/list")
    public PageResult getUserList(String query, Integer currentPage, Integer pageSize) {
        log.info("查询参数:{},{},{}", query, currentPage, pageSize);
        return userListService.getUserList(query, currentPage, pageSize);
    }

    /**
     * 改变用户状态
     */
    @PostMapping("/changeStatus")
    public Result changeStatus(Integer id, Boolean status,String roleNames) {
        log.info("用户id:{},状态:{},角色信息:{}", id, status, roleNames);
        try {
            userListService.changeUserStatus(id, status, roleNames);
        } catch (Exception e) {
            log.error("修改用户状态失败:{}", e.getMessage());
            return Result.error(e.getMessage());
        }
        return Result.ok("操作成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete")
    public Result deleteUser(Integer id, String roleNames) {
        log.info("用户id:{},用户角色{}", id, roleNames);
        try {
            userListService.deleteUser(id, roleNames);
        } catch (Exception e) {
            log.error("删除用户失败:{}", e.getMessage());
            return Result.error(e.getMessage());
        }
        return Result.ok("操作成功");
    }
    /**
     * 更新用户信息
     */
    @PatchMapping("/update")
    public Result updateUser(Integer id,  String email,String password) {
        log.info("用户id:{},邮箱:{},密码:{}", id, email,password);
        try{
            userListService.updateUser(id, email,password);
        }catch (Exception e){
            log.error("更新用户信息失败:{}", e.getMessage());
            return Result.error(e.getMessage());
        }
        return Result.ok("更新成功");
    }
}
